﻿namespace _简单工厂模式
{
    internal class Program
    {
        static void Main(string[] args)
        {



            Operation oper = OperationFactory.createOperate("*");
            if (oper == null)
            {
                Console.WriteLine("未知的运算符");
                return;
            }
            oper.NumberA = 1;
            oper.NumberB = 1;
            Console.WriteLine(oper.GetResult());
        }
    }

    public class Operation
    {
        public double NumberA { get; set; }
        public double NumberB { get; set; }
        public virtual double GetResult()
        {
            return 0;
        }
    }

    /// <summary>
    /// 加法
    /// </summary>
    public class OperationAdd : Operation
    { 
        public override double GetResult()
        {
            return NumberA + NumberB;
        }
    }
    /// <summary>
    /// 减法
    /// </summary>
    public class OperationSub : Operation
    {
        public override double GetResult()
        {
            return NumberA - NumberB;
        }
    }
    /// <summary>
    /// 乘法
    /// </summary>
    public class OperationMul : Operation
    {
        public override double GetResult()
        {
            return NumberA * NumberB;
        }
    }
    /// <summary>
    /// 除法
    /// </summary>
    public class OperationDiv : Operation
    {
        public override double GetResult()
        {
            if (NumberB == 0)
            {
                throw new Exception("除数不能为0");
            }
            return NumberA / NumberB;
        }
    }


    public class OperationFactory
    {
        public static Operation createOperate(string operate)
        {
            Operation oper = null;
            switch (operate)
            {
                case "+":
                    oper = new OperationAdd();
                    break;
                case "-":
                    oper = new OperationSub();
                    break;
                case "*":
                    oper = new OperationMul();
                    break;
                case "/":
                    oper = new OperationDiv();
                    break;
            }
            return oper;
        }
    }
}
